package com.shunbo.convenient.controller;

import com.shunbo.convenient.service.IconService;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.vo.Icon;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author weizhuang
 * 地址
 */
@RestController
@RequestMapping("/icon")
public class IconController {


    @Resource
    private IconService iconService;


    /**
     * 图标列表
     *
     * @param icon
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult getIconList(@RequestBody Icon icon) {
        try {
            List<Icon> all = iconService.findAll(icon);
            return ConvenientResult.build(200, "成功", all);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 添加图标
     *
     * @param icon 图标
     * @return Result结果集
     */
    @RequestMapping("/save")
    public ConvenientResult saveIcon(@RequestBody Icon icon) {
        try {
            Integer save = iconService.save(icon);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 更新图标
     *
     * @param icon 图标
     * @return Result结果集
     */
    @RequestMapping("/update")
    public ConvenientResult updateIcon(@RequestBody Icon icon) {
        try {
            Integer save = iconService.update(icon);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 删除图标
     *
     * @param icon 图标
     * @return Result结果集
     */
    @RequestMapping("/delete")
    public ConvenientResult deleteIcon(@RequestBody Icon icon) {
        try {
            Integer save = iconService.delete(icon.getId() + "");
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * id查图标
     *
     * @param icon 图标
     * @return Result结果集
     */
    @RequestMapping("/findOne")
    public ConvenientResult getIconById(@RequestBody Icon icon) {
        try {
            Icon st = iconService.findById(icon.getId() + "");
            return ConvenientResult.build(200, "成功", st);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }



}
